﻿//-----------------------------------------------------------------------
// <copyright file="SlwyContext.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: SlwyContext.cs
// * history : created by qinchaoyue 2017-11-06 11:12:18
// </copyright>
//-----------------------------------------------------------------------

using Microsoft.AspNetCore.Http;
using Slwy.FrameCore.Dependency;
using Slwy.Utility.Extensions;

namespace Slwy.Context
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// slwy 上下文
    /// </summary>
    public class SlwyContext : Dictionary<string, object>
    {
        /// <summary>
        /// token key
        /// </summary>
        private const string TokenKey = "Slwy_Token";

        /// <summary>
        /// 当前操作者 key
        /// </summary>
        private const string OperatorKey = "Slwy_OperatorKey";

        /// <summary>
        /// 当前环境 key
        /// </summary>
        private const string EnvironmentKey = "Slwy_Environment";

        /// <summary>
        /// 配置文件名字 key
        /// </summary>
        private const string SettingNameKey = "Slwy_SettingName";

        /// <summary>
        /// 上下文集合
        /// </summary>
        private static readonly Lazy<SlwyContext> ContextLazy = new Lazy<SlwyContext>(() => new SlwyContext());

        /// <inheritdoc />
        /// <summary>
        /// 禁止实例化
        /// </summary>
        private SlwyContext()
        {
        }

        /// <summary>
        /// 获取 当前上下文
        /// </summary>
        public static SlwyContext Current => ContextLazy.Value;

        /// <summary>
        /// 获取当前环境
        /// </summary>
        public string Environment
        {
            get
            {
                if (!this.ContainsKey(EnvironmentKey))
                {
                    this[EnvironmentKey] = string.Empty;
                }

                return this[EnvironmentKey].ToString();
            }

            private set => this[EnvironmentKey] = value;
        }

        /// <summary>
        /// 设置 当前环境
        /// </summary>
        /// <param name="environment">当前环境</param>
        public void SetEnvironment(string environment)
        {
            this.Environment = environment;
        }

        /// <summary>
        /// 获取 配置文件名称
        /// </summary>
        public string SettingName
        {
            get
            {
                if (!this.ContainsKey(SettingNameKey))
                {
                    this[SettingNameKey] = string.Empty;
                }

                return this[SettingNameKey].ToString();
            }

            private set => this[SettingNameKey] = value;
        }

        /// <summary>
        /// 设置 配置文件名称
        /// </summary>
        /// <param name="fileName">配置文件名称</param>
        public void SetSettingName(string fileName)
        {
            this.SettingName = fileName;
        }

        /// <summary>
        /// 获取当前操作者
        /// </summary>
        // [Obsolete("严重!! 请使用 Slwy.Mvc.SlwyMvcContext.Operator")]
        public Operator Operator
        {
            get
            {
                var factory = ServiceLocator.Instance.GetService(typeof(IHttpContextAccessor));
                if (factory == null)
                {
                    Utility.DebugLog.Logs.Err("Slwy.Context.Operator :factory is null");
                    return new Operator();
                }

                var context = ((IHttpContextAccessor)factory).HttpContext;
                if (context == null)
                {
                    return new Operator();
                }
                if (context.Items.TryGetValue(OperatorKey, out var op))
                {
                   return op as Operator;
                }
                return new Operator();
            }
        }

        /// <summary>
        /// 设置当前操作者
        /// </summary>
        /// <param name="user"> the user. </param>
        // [Obsolete("严重!! 请使用 Slwy.Mvc.SlwyMvcContext.SetOperator")]
        public void SetOperator(Operator user)
        {
            Utility.DebugLog.Logs.Note("setoperator");

            var factory = ServiceLocator.Instance.GetService(typeof(IHttpContextAccessor));
            if (factory == null)
            {
                Utility.DebugLog.Logs.Err("Slwy.Context.SetOperator :factory is null");
                return;
            }

            var context = ((IHttpContextAccessor)factory).HttpContext;
            if (context == null)
            {
                return;
            }
            context.Items.Remove(OperatorKey);
            context.Items.Add(OperatorKey, user);
        }

        /// <summary>
        /// 获取 token
        /// </summary>
        public string Token
        {
            get
            {
                var factory = ServiceLocator.Instance.GetService(typeof(IHttpContextAccessor));
                if (factory == null)
                {
                    Utility.DebugLog.Logs.Err("Slwy.Context.Token :factory is null");
                    return string.Empty;
                }

                var context = ((IHttpContextAccessor)factory).HttpContext;
                if (context == null)
                {
                    return string.Empty;
                }
                if (context.Items.TryGetValue(TokenKey, out var keyString))
                    return keyString.ToString();
                return string.Empty;
            }
        }

        /// <summary>
        /// 设置 token
        /// </summary>
        /// <param name="token"> the token. </param>
        public void SetToken(string token)
        {
            var factory = ServiceLocator.Instance.GetService(typeof(IHttpContextAccessor));
            if (factory == null)
            {
                Utility.DebugLog.Logs.Err("Slwy.Context.SetToken :factory is null");
                return;
            }

            var context = ((IHttpContextAccessor)factory).HttpContext;
            if (context == null)
            {
                return;
            }
            context.Items.Remove(TokenKey);
            context.Items.Add(TokenKey, token);
        }
    }
}